﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Text;

namespace TreeBuilderTest.Entities
{
    public static class ExtensionMethods
    {
        public static string GenerateGenericArgsString(Type[] arguments)
        {
            var builder = new StringBuilder("<");

            var hasComma = false;

            foreach (Type argument in arguments)
            {
                if (argument.IsGenericType)
                {
                    builder.Append(GenerateGenericArgsString(argument.GetGenericArguments()));
                }

                builder.Append(", ");
                hasComma = true;
            }

            if (hasComma)
            {
                builder.Remove(builder.Length - 2, 2);
            }

            builder.Append(">");

            return builder.ToString();
        }

        public static string GenerateParmString(ParameterInfo[] parameters)
        {
            var builder = new StringBuilder("(");
            var hasComma = false;

            foreach (ParameterInfo parameter in parameters)
            {
                if (parameter.ParameterType.IsByRef)
                {
                    builder.Append("ref ");
                }
                else if (parameter.IsOut)
                {
                    builder.Append("out ");
                }

                builder.Append(parameter.ParameterType.Name);

                if (parameter.ParameterType.IsArray)
                {
                    builder.Append("[]");
                }

                if (parameter.ParameterType.IsGenericType)
                {
                    builder.Append(GenerateGenericArgsString(parameter.ParameterType.GetGenericArguments()));
                }

                if (parameter.IsOptional)
                {
                    builder.Append(" = " + parameter.DefaultValue.ToString());
                }

                builder.Append(", ");
                hasComma = true;
            }

            if (hasComma)
            {
                builder.Remove(builder.Length - 2, 2);
            }

            builder.Append(")");

            return builder.ToString();
        }

        public static string GenerateName(this BaseMethodOperation methodOperation)
        {
            var method = methodOperation.Method;
            var name = method.Name;

            return name + GenerateParmString(method.GetParameters());
        }

        public static string GenerateName(this BaseConstructorOperation constructorOperation)
        {
            var constructor = constructorOperation.Constructor;
            var name = constructor.Name;

            return name + GenerateParmString(constructor.GetParameters());
        }
    }
}
